package com.itheima.entry.factory;

import com.itheima.entry.controller.IStudentController;
import com.itheima.entry.dao.IStudentDAO;
import com.itheima.entry.service.IStudentService;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/*
工厂类,生产需要的对象
方法全静态,方法返值类型为接口名,返回值为接口的实现类
 */
public class StudentFactroy {
    private static Properties prop = new Properties();

    static {
        InputStream reader = ClassLoader.getSystemResourceAsStream("factory.properties");

        try {
            prop.load(reader);
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static IStudentDAO getDao() {
        IStudentDAO o = null;
        try {
            Class<?> clazz = Class.forName(prop.getProperty("IStudentDAO"));
             o = (IStudentDAO) clazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return o;
    }

    public static IStudentService getService() {
        IStudentService o = null;
        try {
            Class<?> clazz = Class.forName(prop.getProperty("IStudentService"));
            o = (IStudentService) clazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return o;
    }

    public static IStudentController getController() {
        IStudentController o = null;
        try {
            Class<?> clazz = Class.forName(prop.getProperty("IStudentController"));
            o = (IStudentController) clazz.newInstance();
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return o;
    }
}
